package com.bj58.mis.apm.server.statistics.controller;

import com.bj58.mis.apm.server.core.common.result.ResultBean;
import com.bj58.mis.apm.server.statistics.dto.chart.ChartData;
import com.bj58.mis.apm.server.statistics.dto.storage.StorageInfo;
import com.bj58.mis.apm.server.statistics.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/storage")
@Api(tags = "存储相关API", description = "/storage")
public class StorageController extends BaseController<StorageService> {

    /**
     * 查询存储-总体情况
     * @return
     */
    @ApiOperation(value = "存储占用量", notes = "查询当前用户有权限的Cube对应的HBase存储和元数据存储量，统计最后两天的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="days", value = "天数", paramType = "query", required = false, dataType = "int")
    })
    @GetMapping(path="/total")
    public ResultBean<List<StorageInfo>> getTotalSize(@RequestParam(name="days", required = false) Integer days){
        if(days == null || days <= 0){
            days = 2;
        }
        List<StorageInfo> list = this.service.getTotalSize(days, getLoginUserName());
        if(list != null && list.size() > 0) {
            Collections.reverse(list);
        }
        return ResultBean.success(list);
    }

    /**
     * 按天统计hbase存储增量，查询当前用户有权限的Cube所占用的HBase存储增量的变化
     * @param days 最近多少天
     * @return
     */
    @ApiOperation(value = "HBase存储增量!", notes = "查询当前用户有权限的Cube所占用的HBase存储增量的变化")
    @ApiImplicitParams({
            @ApiImplicitParam(name="days", value = "天数", paramType = "query", required = false, dataType = "int")
    })
    @GetMapping(path="/hbase")
    public ResultBean<ChartData> getHBaseSize(@RequestParam(name="days", required = false) Integer days){
        if(days == null || days <= 0){
            days = 7;
        }
        return ResultBean.success(this.service.getHBaseSize(days, getLoginUserName()));
    }

    /**
     * 按天统计元数据存储增量，查询当前用户有权限的Cube所占用的元数据存储增量的变化
     * @param days 最近多少天
     * @return
     */
    @ApiOperation(value = "元数据增量", notes = "查询当前用户有权限的Cube所占用的元数据存储增量的变化")
    @ApiImplicitParams({
            @ApiImplicitParam(name="days", value = "天数", paramType = "query", required = false, dataType = "int")
    })
    @GetMapping(path="/metadata")
    public ResultBean<ChartData> getMetadataSize(@RequestParam(name="days", required = false) Integer days){
        if(days == null || days <= 0){
            days = 7;
        }
        return ResultBean.success(this.service.getMetadataSize(days, getLoginUserName()));
    }
}
